package io.geekidea.springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.springboot.entity.Device;
import io.geekidea.springboot.service.IDeviceService;
import io.geekidea.springboot.vo.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/device")
@Tag(name = "设备管理", description = "设备管理接口")
public class DeviceController {

    @Autowired
    private IDeviceService deviceService;

    @PostMapping
    @Operation(summary = "新增设备")
    public ResponseResult<Device> add(@Valid @RequestBody Device device) {
        device.setId(null);
        deviceService.save(device);
        return ResponseResult.success(device);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除设备")
    public ResponseResult<Void> delete(@PathVariable Long id) {
        deviceService.removeById(id);
        return ResponseResult.success();
    }

    @PutMapping
    @Operation(summary = "更新设备")
    public ResponseResult<Device> update(@Valid @RequestBody Device device) {
        deviceService.updateById(device);
        return ResponseResult.success(device);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询设备详情")
    public ResponseResult<Device> getById(@PathVariable Long id) {
        Device device = deviceService.getById(id);
        return ResponseResult.success(device);
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询设备列表")
    public ResponseResult<Page<Device>> list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Long buildingId) {
        Page<Device> result = deviceService.listByPage(page, size, buildingId);
        return ResponseResult.success(result);
    }
}
